const Koa = require('koa')
const router = require('koa-router')()
const app = new Koa()
const body = require('koa-body')
const Mock = require('mockjs')
const { exec } = require('child_process')

Mock.Random.extend({
  giftCode: function() {
    return Math.random() > 0.5 ? Mock.Random.word() : ''
  }
})

// ajax
router.post('/sdk/float/user/index', body(), (ctx, next) => {
  // let req = ctx.request.body
  let res = {
    code: 200,
    msg: 'success',
    data: Mock.mock({
      mem_id: 1,
      avatar: '@image("80x80")',
      username: '@name()',
      nickname: '@name()',
      my_integral: 100,
      gift_cnt: 20,
      game_cnt: 5,
      has_msg: 1,
      has_identify: 1,
      has_bind_mobile: 1,
      sign_days: 0,
      has_sign: 1
    })
  }
  ctx.body = res
})

router.post('/sdk/float/user/nickname/update', body(), (ctx, next) => {
  let res = {
    code: 200,
    msg: 'success'
  }
  ctx.body = res
})

router.post('/sdk/float/user/passwd/update', body(), (ctx, next) => {
  let res = {
    code: 200,
    msg: 'success'
  }
  ctx.body = res
})

router.get('/sdk/float/gift/list', body(), (ctx, next) => {
  let req = ctx.request.body
  let res = {
    code: 200,
    msg: 'success',
    data: Mock.mock({
      count: 10,
      'list|8': [{
        'gift_id|+1': 1,
        'game_id': 1,
        'title': '@name()',
        'total_cnt': 200,
        'remain_cnt': 100,
        'content': '@name()',
        'icon': '@image("200x200")',
        'start_time': '@date()',
        'end_time': '@date()',
        'dead_time': '@date()',
        'gift_code': '@giftCode()',
        'scope': '仅限《崩坏3》风火轮渠道玩家领取',
        'func': '登入游戏—在【赏品兑换】中输入兑换码',
        'condition': 10
      }]
    })
  }
  ctx.body = res
})

router.get('/sdk/float/gift/detail', body(), (ctx, next) => {
  let res = {
    code: 200,
    msg: 'success',
    data: Mock.mock({
      gift_id: ctx.query.gift_id,
      game_id: 1,
      title: '@name()',
      total_cnt: 300,
      remain_cnt: 120,
      content: '@sentence()',
      icon: '@image("80x80")',
      start_time: '@date()',
      end_time: '@date()',
      dead_time: '@date()',
      gift_code: '@giftCode()',
      scope: '仅限《崩坏3》风火轮渠道玩家领取',
      func: '登入游戏—在【赏品兑换】中输入兑换码',
      condition: 10
    })
  }
  ctx.body = res
})

router.post('/sdk/float/user/gift/add', body(), (ctx, next) => {
  let req = ctx.request.body
  let res = {
    code: 200,
    msg: 'success',
    data: Mock.mock({
      gift_id: req.gift_id,
      game_id: 1,
      title: '@name()',
      total_cnt: 300,
      remain_cnt: 120,
      content: '@sentence()',
      icon: '@image("80x80")',
      start_time: '@date()',
      end_time: '@date()',
      dead_time: '@date()',
      gift_code: 'ssf323sf2sdf',
      scope: '仅限《崩坏3》风火轮渠道玩家领取',
      func: '登入游戏—在【赏品兑换】中输入兑换码',
      condition: 10
    })
  }
  ctx.body = res
})

router.get('/float/help/index', body(), (ctx, next) => {
  let res = {
    code: 200,
    msg: 'success',
    data: Mock.mock({
      qq: ['12323423', '23423424', '234535345'],
      qqgroup: ['23322535'],
      wx: '',
      tel: '020-23423433',
      service_time: '07:00 - 22:00',
      weibo: 'xxxx',
      officesite: ''
    })
  }
  ctx.body = res
})

router.get('/sdk/float/news/list', body(), (ctx, next) => {
  let res = {
    code: 200,
    msg: 'success',
    data: Mock.mock({
      count: 8,
      'list|8': [{
        'news_id|+1': 1,
        'game_id': 1,
        'title': '@name()',
        'img': '@image("200x200")',
        'pub_time': '@date()',
        'type': ctx.query.type,
        'start_time': '@date()',
        'end_time': '@date()',
        'url': ''
      }]
    })
  }
  ctx.body = res
})

router.get('/sdk/float/news/detail', body(), (ctx, next) => {
  let res = {
    code: 200,
    msg: 'success',
    data: Mock.mock({
      news_id: ctx.query.news_id,
      title: '@name()',
      game_id: 1,
      pub_time: '@date()',
      img: '@image("200x200")',
      type: 3,
      start_time: '@date()',
      end_time: '@date()',
      content: '@sentence()',
      url: ''
    })
  }
  ctx.body = res
})

router.get('/sdk/float/msg/list', body(), (ctx, next) => {
  let res = {
    code: 200,
    msg: 'success',
    data: Mock.mock({
      count: 21,
      'list|8': [{
        'id|+1': 1,
        'gameid': 1,
        'title': '@name()',
        'readed': 1,
        'type': ctx.query.type,
        'create_time': '@date()',
        'content': '@sentence()'
      }]
    })
  }
  ctx.body = res
})

router.get('/sdk/float/msg/detail', body(), (ctx, next) => {
  let res = {
    code: 200,
    msg: 'success',
    data: Mock.mock({
      id: ctx.query.id,
      gameid: 1,
      title: '@name()',
      readed: 1,
      type: ctx.query.type,
      create_time: '@date()',
      content: '@sentence()'
    })
  }
  ctx.body = res
})

app.use(router.routes())

app.listen(9080, () => {
  console.log('server running at http://localhost:9080/')
})

exec('npm run dev', (error, stdout, stderr) => {
  if (error) {
    console.error(`exec error: ${error}`)
    return
  }
  console.log(`stdout: ${stdout}`)
  console.log(`stderr: ${stderr}`)
})
